package com.resou.reader.mine.coupon.fragment;

import android.arch.lifecycle.Lifecycle;
import android.arch.lifecycle.LifecycleOwner;
import android.support.annotation.NonNull;
import android.util.Log;

import com.resou.reader.api.ApiImp;
import com.resou.reader.api.entry.CouponBean;
import com.resou.reader.api.entry.GenderCategory;
import com.resou.reader.api.entry.ResultList;
import com.resou.reader.api.service.HotTicketService;
import com.resou.reader.base.ResouBasePresenter;
import com.resou.reader.base.m.IModel;
import com.resou.reader.base.p.BasePresenter;
import com.resou.reader.mine.IView.UserInstance;
import com.resou.reader.utils.UserUtil;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import rx.android.MainThreadSubscription;

/**
 * Author: 萤火虫
 * E-mail:  Allensuns@163.com
 * Description:
 * CreateTime: 2019-03 -19
 **/
public class FragmentCouponPresenter extends ResouBasePresenter<FragmentCouponContract.ICouponView> implements FragmentCouponContract.ICouponPresenter {
    private static final String TAG = "FragmentCouponPresenter";
    HotTicketService hotTicketService;

    public FragmentCouponPresenter(FragmentCouponContract.ICouponView iCouponView) {
        super(iCouponView);
        this.hotTicketService = ApiImp.getInstance().getService(HotTicketService.class);
    }


    @Override
    public synchronized void quearCouponData(final int page, int type) {
        hotTicketService.quearHotTicket(UserUtil.getToken(), type, page, pageSize)
                .subscribeOn(getIOSchedulers())
                .observeOn(getMainSchedulers())
                .subscribe(new Observer<ResultList<CouponBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addCompositeDisposable(d);
                    }

                    @Override
                    public void onNext(ResultList<CouponBean> genderCategoryResultList) {
                        Log.i(TAG, "onNext: " + genderCategoryResultList.getData());
                        if (genderCategoryResultList.getData().size() > 0) {
                            mView.getmSmartRefreshLayout().finishLoadMore();
                            if (genderCategoryResultList.getData().size() < 15) {
                                mView.getmSmartRefreshLayout().finishLoadMoreWithNoMoreData();
                            }
                            mView.showCouponData(genderCategoryResultList.getData());
                        } else {
                            Log.i(TAG, "onNext:page" + page);
                            if (page == 0) {
                                mView.showEmpty();
                                mView.getmSmartRefreshLayout().finishRefresh();
                            }
                            mView.getmSmartRefreshLayout().finishLoadMoreWithNoMoreData();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showError(e);
                    }

                    @Override
                    public void onComplete() {
                        Log.i(TAG, "onComplete: ");
                    }
                });
    }

}
